package com.kaiwukj.android.ufamily.mvp.ui.page.service.gm;

import android.graphics.Color;
import android.util.SparseBooleanArray;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMOrderDetailResult;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class GMOrderDetailVideoIndexAdapter extends BaseQuickAdapter<GMOrderDetailResult.VOResult, BaseViewHolder> {

    private SparseBooleanArray sparseBooleanArray;
    private int laseCheckPosition = -1;

    public GMOrderDetailVideoIndexAdapter(@Nullable List<GMOrderDetailResult.VOResult> data) {
        super(R.layout.item_gm_order_detail_video_pos, data);
        sparseBooleanArray = new SparseBooleanArray(getDefItemCount());
    }


    public void checkItem(int position) {
        if (position == laseCheckPosition) return;
        if (laseCheckPosition != -1) {
            sparseBooleanArray.put(laseCheckPosition, false);
            notifyItemChanged(laseCheckPosition);
        }
        sparseBooleanArray.put(position, true);
        notifyItemChanged(position);
        laseCheckPosition = position;
        if (mListener != null) {
            mListener.onItemChecked(laseCheckPosition);
        }
    }

    public int getCheckPosition() {
        return laseCheckPosition;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, GMOrderDetailResult.VOResult voResult) {
        QMUIRoundButton button = holder.getView(R.id.view_pos);
        button.setText(voResult.getDeviceRemarks());
        if (sparseBooleanArray.get(holder.getAdapterPosition())) {
            button.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.app_color_theme));
            button.setTextColor(ContextCompat.getColor(getContext(), R.color.text_white));
        } else {
            button.setBackgroundColor(Color.parseColor("#F7F7F7"));
            button.setTextColor(ContextCompat.getColor(getContext(), R.color.text_01));
        }

        holder.itemView.setOnClickListener(v -> checkItem(holder.getAdapterPosition()));
    }

    private OnCheckChangedListener mListener;

    public void setOnCheckChangedListener(OnCheckChangedListener listener) {
        mListener = listener;
    }

    public interface OnCheckChangedListener {
        void onItemChecked(int position);
    }
}
